//
//  LoginInputView.swift
//  maosheng
//
//  Created by 何海涛 on 2019/8/11.
//

import Foundation
import UIKit

protocol LoginInputViewDelegate {
    func sendValidateCode()
}

class LoginInputView<TLabel:UIView>: UIView, IStandardTimedEventHandler {
    
    lazy var icon: TLabel = {
        var lbl = TLabel()
        self.addSubview(lbl)
        lbl.snp.makeConstraints({ (make) in
            make.left.centerY.equalTo(self)
        })
        return lbl
    }()
    
    lazy var txt: UITextField = {
        var txt = UITextField()
        txt.font = VS.font18
        self.addSubview(txt)
        txt.snp.makeConstraints({ (make) in
            make.left.equalTo(self.icon.snp.right).offset(10)
            make.centerY.equalTo(self)
        })
        return txt
    }()
    
    fileprivate lazy var btn: UIButton = {
        var btn = UIButton()
        btn.titleLabel?.font = VS.font12
        btn.setTitleColor(VS.assistRed, for: .normal)
        btn.setTitleColor(VS.assistRed.toNewAlpha(0.8), for: .disabled)
        btn.setTitle("获取验证码", for: .normal)
        btn.addTarget(self, action: #selector(btnSendClicked), for: .touchUpInside)
        self.addSubview(btn)
        btn.snp.makeConstraints({ (make) in
            make.right.centerY.equalTo(self)
            make.width.equalTo(80)
        })
        return btn
    }()
    
    fileprivate var timer: StandardTimedEvent!
    fileprivate var counter: Int = 60 // 60 seconds
    var delegate: LoginInputViewDelegate?
    
    func render(text: String, placeHolder: String, keyboard: UIKeyboardType, showSend: Bool) {
        renderLabel(text: text)
        txt.placeholder = placeHolder
        txt.keyboardType = keyboard
        
        resetSendBtn(show: showSend)
    }
    
    func renderLabel(text: String) {
        
    }
    
    func resetSendBtn(show: Bool) {
        btn.isHidden = !show
        
        var expectedWidth = CommonDefine.kScreenWidth - 40 * 2 - 25;
        if show { expectedWidth -= 80 }
        txt.snp.makeConstraints { (make) in
            make.width.equalTo(expectedWidth)
        }
    }
    
    @objc
    func btnSendClicked() {
//     let phoneField = self.superview
        delegate?.sendValidateCode()

    }
    func checkPhoneValid(phoneNum : String) -> Bool{
        var isValid = false
        if phoneNum.count == 11 {
            isValid = true
        }else{
            ProgressHUD.showMsgWithoutView("手机号码不正确")
        }
        return isValid
    }
    func beginCoundown(){
        btn.isEnabled = false
        timer = StandardTimedEvent(interval: 1)
        timer += self
    }
    
    func scheduleTimerFired(timer: Timer) {
        counter -= 1
        if counter <= 0 {
            // reset all resource
            self.timer.stop()
            btn.isEnabled = true
            counter = 60
        } else {
            let text = "\(counter)s可再发送"
            btn.setTitle(text, for: .disabled)
        }
    }
}

class LoginInputImageView: LoginInputView<UIImageView> {
    
    override func renderLabel(text: String) {
        self.icon.image = UIImage(named: text)
    }
}

class LoginInputLabelView: LoginInputView<UILabel> {
    
    override func renderLabel(text: String) {
        self.icon.text = text;
        self.icon.font = VS.font18
    }
}
